package cn.android.weblog;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.android.weblog.adapter.PassageListAdapter;
import cn.android.weblog.api.CatagoryAPI;
import cn.android.weblog.api.PassageAPI;
import cn.android.weblog.domain.Catagory;
import cn.android.weblog.domain.Keyword;
import cn.android.weblog.domain.Passage;
import cn.android.weblog.redisdata.CURRENT;
import cn.android.weblog.redisdata.LOGIN;

public class MyCatagoryActivity extends AppCompatActivity {


    private List<Passage> passage;
    private CatagoryAPI catagoryAPI;
    private PassageListAdapter adapter;
    private ListView lv;
    private TextView title;
    private ImageView back;
    private Button add;
    private PopupMenu pm;
    private PassageAPI passageAPI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_catagory);

        catagoryAPI=new CatagoryAPI();
        adapter=new PassageListAdapter(getApplicationContext());
        lv=(ListView)findViewById(R.id.mycatagory_lv);
        title=(TextView)findViewById(R.id.mycatagory_title);
        back=(ImageView)findViewById(R.id.mycatagory_back);
        add=(Button)findViewById(R.id.mycatagory_add);
        passageAPI=new PassageAPI();

        // 单击分类时显示关键词下拉列表
        title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pm.show();
            }
        });

        // 初始化
        init();
        initMenu();

        // 返回上一页
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // 单击进入文章详情页
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 将选中的文章信息存入缓存域中
                CURRENT.PASSAGE=(Passage)adapter.getItem(position);
                Intent intent=new Intent(MyCatagoryActivity.this,PassageDetailActivity.class);
                startActivity(intent);
            }
        });

        // 添加新的分类
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCatagory();
            }
        });


    }

    /**
     * 添加新的分类对话框
     */
    private void addCatagory(){
        final EditText inputServer = new EditText(this);
        inputServer.setFocusable(true);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请输入要添加分类的名称")
                .setView(inputServer)
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        String inputName = inputServer.getText().toString();
                        Catagory c=new Catagory();
                        c.setCatagoryName(inputName);
                        // 添加分类
                        String msg = catagoryAPI.addCatagory(c, LOGIN.USER.getUserId());
                        Toast.makeText(MyCatagoryActivity.this,msg,Toast.LENGTH_SHORT).show();
                        // 刷新分类
                        if(msg.equals("添加成功")){
                            initMenu();
                        }
                    }
        });
        builder.show();
    }

    /**
     * 初始化页面
     */
    private void init(){
        passage=passageAPI.findPassageByUserId(LOGIN.USER.getUserId());
        adapter.setData(passage);
        lv.setAdapter(adapter);
    }

    /**
     * 初始化我的分类下拉框
     */
    private void initMenu(){
        pm=new PopupMenu(MyCatagoryActivity.this,title);
        Menu menu=pm.getMenu();
        pm.getMenuInflater().inflate(R.menu.my_catagory,menu);
        List<Catagory> lists = catagoryAPI.findCatagoryByUserId(LOGIN.USER.getUserId());
        menu.add(0,4719,0,"所有文章");
        // 将分类全部添加到菜单项中
        for(Catagory c:lists){
            // 1-组别、2-菜单项id、3-菜单项顺序、4-菜单项内容
            menu.add(0,c.getCatagoryId(),0,c.getCatagoryName());
        }
        // 添加单击数据项事件
        pm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                // 当选择某一个分类的时候，更新页面值
                changePage(item);
                return true;
            }
        });
    }

    /**
     * 当选择分类后更新页面
     */
    private void changePage(MenuItem item){
        if(item.getItemId() == 4719) {
            // 所有文章
            init();
            title.setText("首页");
        }else {
            // 查询关键词下文章列表
            List<Passage> lists = catagoryAPI.findPassageByCatagoryId(item.getItemId());
            // 刷新页面 (只有adapter重新new才能成功更新页面)
            adapter = new PassageListAdapter(getApplicationContext());
            adapter.setData(lists);
            lv.setAdapter(adapter);
            // 更新页面标题
            title.setText(item.getTitle());
        }
    }


}
